var uuid = require('node-uuid');
var consts = require('../../../util/consts');
var Help = require('../../../domain/help');
var Feedback = require('../../../domain/feedback');
var utils = require('../../../util/utils');
var moment = require('moment');

module.exports = function (app) {
    return new Handler(app);
};

var Handler = function (app) {
    this.app = app;
};

var handler = Handler.prototype;

handler.helpList = function (msg, session, next) {
    // var pageNum = msg.pageNum || 1;
    // var pageSize = msg.pageSize || 10;
    Help.find({}, {_id: 0, __v: 0, gmt_create: 0, gmt_modified: 0}, function (err, helps) {
        if(err){
            next(null, {
                code: consts.Code.ERR,
                msg: consts.Msg.QUERY_FAILED
            });
            return;
        }
        next(null, {
            code: consts.Code.SUC,
            helps: helps
        });
    }); //.skip((pageNum - 1) * pageSize).limit(pageSize);
};

handler.helpInfo = function (mgs, session, next) {
    var id = mgs.id;
    if (!id) {
        next(null, {
            code: consts.Code.ERR,
            msg: consts.Msg.USER_NOT_FOUND
        });
    }
    
    Help.findOne({id: id}, {_id: 0, __v: 0, gmt_modified: 0}, function (err, help) {
        if(err){
            next(null, {
                code: consts.Code.ERR,
                msg: consts.Msg.QUERY_FAILED
            });
            return;
        }

        var time = moment(help.gmt_create).format('YYYY-MM-DD HH:mm');
        next(null, {
            code: consts.Code.SUC,
            help: {
                id: help.id,
                title: help.title,
                content: help.content,
                time: time
            }
        });
    });
};

handler.getFeedList = function(mgs, session, next){
    var uid = session.get('uid');
    if (!uid) {
        next(null, {
            code: consts.Code.ERR,
            msg: consts.Msg.USER_NOT_FOUND
        });
        return
    }

    Feedback.find({uid: uid}, {_id: 0, __v: 0, gmt_create: 0, gmt_modified: 0}, function (err, data) {
        if(err){
            next(null, {
                code: consts.Code.ERR,
                msg: consts.Msg.QUERY_FAILED
            });
            return
        }
        var list = [];
        for (var i=0;i<data.length;i++){
            list.push({
                id: data[i].id,
                uid: data[i].uid,
                title: data[i].content_list[0].title,
                content_time: moment(data[i].content_list[0].content_time).format('YYYY-MM-DD HH:mm')
            });
        }
        next(null, {
            code: consts.Code.SUC,
            data: list
        });
    });
};

handler.getFeedInfo = function(mgs, session, next){
    var uid = session.get('uid');
    var id = mgs.id;
    if (!uid || !id) {
        next(null, {
            code: consts.Code.ERR,
            msg: consts.Msg.USER_NOT_FOUND
        });
        return
    }
    Feedback.findOne({uid: uid, id: id}, {_id: 0, __v: 0, gmt_create: 0, gmt_modified: 0}, function (err, data) {
        if(err){
            next(null, {
                code: consts.Code.ERR,
                msg: consts.Msg.QUERY_FAILED
            });
            return
        }
        var list = [];
        for (var i=0;i<data.content_list.length;i++){
            list.push({
                type: data.content_list[i].type,
                title: data.content_list[i].title,
                content: data.content_list[i].content,
                content_time: moment(data.content_list[i].content_time).format('YYYY-MM-DD HH:mm')
            });
        }
        next(null, {
            code: consts.Code.SUC,
            data: {
                id: data.id,
                uid: data.uid,
                content_list: list
            }
        });
    });
};

handler.feedback = function(mgs, session, next){
    var uid = session.get('uid');
    var title = mgs.title;
    var content = mgs.content;
    if (!uid || !content || !title) {
        next(null, {
            code: consts.Code.ERR,
            msg: consts.Msg.USER_NOT_FOUND
        });
        return
    }

    var newFeedback = new Feedback({
        id: uuid.v4(),
        uid: uid,
        content_list: [{
            title: title,
            content: content,
            type: 1
        }]
    });
    newFeedback.save(function (err, data) {
        if (err){
            next(null, {
                code: consts.Code.ERR,
                msg: consts.Msg.SUBMIT_ERR
            });
            return;
        }
        var list = [];
        for (var i=0;i<data.content_list.length;i++){
            list.push({
                type: data.content_list[i].type,
                title: data.content_list[i].title,
                content: data.content_list[i].content,
                content_time: moment(data.content_list[i].content_time).format('YYYY-MM-DD HH:mm')
            });
        }
        next(null, {
            code: consts.Code.SUC,
            data: {
                id: data.id,
                uid: data.uid,
                content_list: list
            }
        });
    });
};

// handler.addHelp = function (mgs, session, next) {
//     var newHelp = new Help({
//         id: uuid.v4(),
//         // 我的钱包在哪？
//         title: '我的邀请码可以分享几个人？',
//         content: '我的邀请码可以分享几个人？'
//     });
//     newHelp.save(function (err, userData) {
//         if (err){
//             console.log('addHelp err = ' + err);
//         }
//     })
// };